﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class FormClient : Form
    {
        public Client Client { get; set; }

        public FormClient()
        {
            InitializeComponent();
            Text = Forms.CLIENT_FORM;
        }
        //konstruktor inicjujący formatkę danymi konkretnego klienta
        public FormClient(Client client)
        {
            InitializeComponent();
            if (client != null)
            {
                fillFields(client);

            }

            btnNew.Enabled = true;
            Text = Forms.CLIENT_FORM;
        }
        private void fillFields(Client client)
        {
            int sur_pos = client.Name.LastIndexOf(' ');
            this.Client = client;
            // rozdzielenie imienia od nazwiska (w bazie jedno pole)
            if (sur_pos > 0)
            {
                txtName.Text = client.Name.Substring(0, sur_pos);
                txtSurname.Text = client.Name.Substring(sur_pos + 1);
            }
            else
            {
                txtName.Text = client.Name;
                txtSurname.Text = "";
            }
            cmbCountry.Text = client.Country;//w przyszlosci zmienic na SelecteText
            txtId.Text = client.Id.ToString();
            txtCity.Text = client.City;
            txtStreet.Text = client.Street;
            txtZip.Text = client.Zip;
            txtFlatNumber.Text = client.FlatNumber;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.Client = null;
            this.txtCity.Text = "";
            this.txtFlatNumber.Text = "";
            this.txtId.Text = "";
            this.txtName.Text = "";
            this.txtStreet.Text = "";
            this.txtSurname.Text = "";
            this.txtZip.Text = "";
            this.cmbCountry.SelectedText = "";
        }

        private String validateFields()
        {
            String fields = "";
            fields += txtName.Text.Equals(String.Empty) ? "imię, " : "";
            fields += txtSurname.Text.Equals(String.Empty) ? "nazwisko, " : "";
            fields += cmbCountry.Text.Equals(String.Empty) ? "państwo, " : "";//pozniej zmienic na SelectedText
            fields += txtCity.Text.Equals(String.Empty) ? "miasto, " : "";
            fields += txtStreet.Text.Equals(String.Empty) ? "ulica, " : "";
            fields += txtZip.Text.Equals(String.Empty) ? "kod, " : "";
            fields += txtFlatNumber.Text.Equals(String.Empty) ? "numer, " : "";

            return fields;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            String fields = validateFields();
            if (!fields.Equals(String.Empty))
            {
                MessageBox.Show("Proszę uzupełnić pola " + fields.Substring(0, fields.Length - 2) + ".");
                return;
            }

            // imie i nazwisko
            if (this.Client == null && ServiceFactory.getClientService().checkIfNameExists(txtName.Text + " " + txtSurname.Text) > 0 )
            {
                MessageBox.Show("Podany klient już istnieje w bazie!");
                return;
            }

            //przy zapisywaniu najpierw tworzymy obiekt modelu a potem przekazujemy do odpowiedniego serwisu
            Client client = this.Client == null ? new Client() : this.Client;
            client.Name = txtName.Text + " " + txtSurname.Text;
            client.Country = cmbCountry.Text;//zmienic w przyszlosci
            client.City = txtCity.Text;
            client.Street = txtStreet.Text;
            client.Zip = txtZip.Text;
            client.FlatNumber = txtFlatNumber.Text;

            ServiceFactory.getClientService().saveClient(client);

            MessageBox.Show("Dodano klienta " + client.Name);

        }

        private void FormClient_Load(object sender, EventArgs e)
        {
        }
    }
}
